<project name="KindEditor" default="min" basedir=".">

	<description>
		KindEditor build file
	</description>

	<property name="src" value="src"/>
	<property name="lang" value="lang"/>
	<property name="plugins" value="plugins"/>
	<property name="filename" value="kindeditor"/>
	<property name="version" value="4.1.4"/>
	<property name="charset" value="utf-8"/>
	<property name="dist" value="dist/${filename}-${version}"/>

	<target name="merge">
		<tstamp>
			<format property="thisyear" pattern="yyyy"/>
			<format property="ymd" pattern="yyyy-MM-dd"/>
		</tstamp>
		<concat destfile="${filename}.js"
			encoding="${charset}"
			outputencoding="${charset}">
			<fileset file="${src}/header.js"/>
			<fileset file="${src}/core.js"/>
			<fileset file="${src}/config.js"/>
			<fileset file="${src}/event.js"/>
			<fileset file="${src}/html.js"/>
			<fileset file="${src}/selector.js"/>
			<fileset file="${src}/node.js"/>
			<fileset file="${src}/range.js"/>
			<fileset file="${src}/cmd.js"/>
			<fileset file="${src}/widget.js"/>
			<fileset file="${src}/edit.js"/>
			<fileset file="${src}/toolbar.js"/>
			<fileset file="${src}/menu.js"/>
			<fileset file="${src}/colorpicker.js"/>
			<fileset file="${src}/uploadbutton.js"/>
			<fileset file="${src}/dialog.js"/>
			<fileset file="${src}/tabs.js"/>
			<fileset file="${src}/ajax.js"/>
			<fileset file="${src}/main.js"/>
			<fileset file="${src}/footer.js"/>
		</concat>
		<replaceregexp file="${filename}.js"
			match="\$\{VERSION\}"
			replace="${version} (${ymd})"
			encoding="${charset}"
			byline="true"
			flags="g"
		/>
		<replaceregexp file="${filename}.js"
			match="\$\{THISYEAR\}"
			replace="${thisyear}"
			encoding="${charset}"
			byline="true"
			flags="g"
		/>
		<replaceregexp file="${filename}.js"
			match="/\*\*(\r\n|\n|\r).*?\*/"
			replace=""
			encoding="${charset}"
			byline="false"
			flags="gs"
		/>
		<replaceregexp file="${filename}.js"
			match="(^|\s)//.*$"
			replace=""
			encoding="${charset}"
			byline="true"
			flags="g"
		/>
		<replaceregexp file="${filename}.js"
			match="(\r\n|\n|\r)/\*\*/.*(\r\n|\n|\r)"
			replace="\1"
			encoding="${charset}"
			byline="false"
			flags="g"
		/>
		<replaceregexp file="${filename}.js"
			match="\s+$"
			replace=""
			encoding="${charset}"
			byline="true"
			flags="g"
		/>
		<replaceregexp file="${filename}.js"
			match="(\r\n){2,}"
			replace="\1"
			encoding="${charset}"
			byline="false"
			flags="g"
		/>
	</target>

	<target name="check" depends="merge">
		<exec executable="java">
			<arg line="-jar lib/jslint/js.jar lib/jslint/jslint-check.js"/>
		</exec>
	</target>

	<target name="min" depends="check">
		<exec executable="java">
			<arg line="-jar lib/closure-compiler.jar --js=${filename}.js --js_output_file=${filename}-min.js"/>
		</exec>
		<replaceregexp file="${filename}-min.js"
			match="^"
			replace="/* KindEditor ${version} (${ymd}), Copyright (C) kindsoft.net, Licence: http://www.kindsoft.net/license.php */"
			encoding="${charset}"
			byline="false"
		/>
	</target>

	<target name="all" depends="min">
		<concat destfile="${filename}-all.js"
			encoding="${charset}"
			outputencoding="${charset}">
			<fileset file="${filename}.js"/>
			<fileset file="${lang}/zh_CN.js"/>
			<fileset file="${plugins}/anchor/anchor.js"/>
			<fileset file="${plugins}/baidumap/baidumap.js"/>
			<fileset file="${plugins}/clearhtml/clearhtml.js"/>
			<fileset file="${plugins}/code/code.js"/>
			<fileset file="${plugins}/emoticons/emoticons.js"/>
			<fileset file="${plugins}/filemanager/filemanager.js"/>
			<fileset file="${plugins}/flash/flash.js"/>
			<fileset file="${plugins}/image/image.js"/>
			<fileset file="${plugins}/insertfile/insertfile.js"/>
			<fileset file="${plugins}/lineheight/lineheight.js"/>
			<fileset file="${plugins}/link/link.js"/>
			<fileset file="${plugins}/map/map.js"/>
			<fileset file="${plugins}/media/media.js"/>
			<fileset file="${plugins}/multiimage/multiimage.js"/>
			<fileset file="${plugins}/pagebreak/pagebreak.js"/>
			<fileset file="${plugins}/plainpaste/plainpaste.js"/>
			<fileset file="${plugins}/preview/preview.js"/>
			<fileset file="${plugins}/quickformat/quickformat.js"/>
			<fileset file="${plugins}/table/table.js"/>
			<fileset file="${plugins}/template/template.js"/>
			<fileset file="${plugins}/wordpaste/wordpaste.js"/>
		</concat>
		<exec executable="java">
			<arg line="-jar lib/closure-compiler.jar --js=${filename}-all.js --js_output_file=${filename}-all-min.js"/>
		</exec>
		<replaceregexp file="${filename}-all-min.js"
			match="^"
			replace="/* KindEditor ${version} (${ymd}), Copyright (C) kindsoft.net, Licence: http://www.kindsoft.net/license.php */"
			encoding="${charset}"
			byline="false"
		/>
	</target>

	<target name="zip" depends="all">
		<mkdir dir="dist"/>
		<delete dir="${dist}"/>
		<delete file="${dist}.zip"/>
		<mkdir dir="${dist}"/>
		<copy todir="${dist}">
			<fileset dir=".">
				<include name="asp.net/**"/>
				<include name="asp/**"/>
				<include name="attached"/>
				<include name="examples/**"/>
				<include name="jsp/**"/>
				<include name="lang/**"/>
				<include name="php/**"/>
				<include name="plugins/**"/>
				<include name="themes/**"/>
				<include name="kindeditor*.js"/>
				<include name="license.txt"/>
			</fileset>
		</copy>
		<zip destfile="${dist}.zip">
			<zipfileset dir="${dist}" prefix="${filename}-${version}"/>
		</zip>
	</target>

</project>
